let url = project + '/onlineArchive/outsourcing';
Axios.defaults.baseURL = url;

let main ={
    el:"#app",
    data:function() {
        return {
            uploadExcelUrl:'/operation/onlineArchive/outsourcing/task/uploadExcel',
            uploadFileUrl:'/operation/onlineArchive/outsourcing/task/uploadAttachment',
            loading_1:false,
            activeName: 'first',
            progressPercent:0,
            progressStatus:'success',
            fileNumber:0,
            closeDialogButton:false,
            msgList:["---开始---"],
            packComs:[],
            fileHeader:{
                relationId:0
            },
            taskList:[],
            taskSearch:{
                pagination:{
                    pageNum: 1,
                    pageSize: 10,
                    total:0,
                },
                taskEndDate:[]
            },
            outerVisible :false,
            innerVisible:false,
            task:{
                packComId:'',
                deptName:'',
                taskEndTime:'',
                description:''
            },
            fileList: [],
            excelList:[],
            viewFileList:[],
            dialogTableVisible:false,
            rules:{
                packComId:[{ required: true, message: '请输入外包商名称', trigger: 'change' }],
                taskEndTime:[{ required: true, message: '请选择移交时间', trigger: 'change' }],
            },
            permissions:false,
            uploadVisible:false,
            uploadFileList:[],
            dialogTableDeleteVisible:false
        }
    },
    beforeMount:function(){
        this.init();
    },
    methods:{
        init:function(){
            let that = this;
            axios.all([getOrgName(),that.getPermissions()])
                .then(axios.spread(function (orgName,permissions) {
                    that.task.deptName = orgName.data.data;
                    that.permissions = permissions.data.data;
                })).then(function () {
                that.initTaskPageList();
            });
        },
        /**
         * 上传附件
         * @param row
         */
        uploadAttachment:function(row){
            this.fileHeader.relationId = row.id;
            this.uploadFileList = [];
            this.uploadVisible = true;
        },
        /**
         * 获取按钮权限
         * @returns {*}
         */
        getPermissions:function(){
            return axios.get(url + '/task/permissions');
        },
        /**
         * 获取外包商信息
         */
        getPackerList:function(){
            let that =this;
            Axios.get('/task/packerList')
                .then(function (response) {
                    if (response.code != 0){
                        noticeError(that,'外包商信息获取失败');
                        return;
                    }
                    that.packComs = response.data;
                }).catch(function (error) {
                    noticeDataRequestFailure(that);
            })
        },
        /**
         *获取待整理数据
         */
        getTaskPageList:function(){
            let that = this;
            that.loading_1 = true;
            Axios({
                method: 'post',
                url: '/task/pageList',
                data: that.taskSearch
            })
                .then(function (response) {
                    if (response.code != 0){
                        noticeDataRequestFailure(that);
                        that.loading_1 = false;
                        return;
                    }
                    that.taskList = response.data.list;
                    that.taskSearch.pagination.total = response.data.total;
                    that.loading_1 = false;
                })
                .catch(function (error) {
                    noticeFailure(that);
                    that.loading_1 = false;
                });
        },
        /**
         * 初始化待整理数据列表，（加载第一页数据）
         */
        initTaskPageList:function(){
            this.taskTablePageNumChange(1);
        },
        /**
         * 待整理列表 ，页码改变时触发
         * @param pageNum
         */
        taskTablePageNumChange:function(pageNum){
            this.taskSearch.pagination.pageNum = pageNum;
            this.getTaskPageList();
        },
        /**
         * 待整理数据列表，每页条数改变时触发
         * @param pageSize
         */
        taskTablePageSizeChange:function(pageSize){
            this.taskSearch.pagination.pageNum = 1;
            this.taskSearch.pagination.pageSize = pageSize;
            this.getTaskPageList();
        },
        /**
         * 新建任务
         */
        newTask:function(){
            this.task.packComId = '';
            this.task.taskEndTime = '';
            this.task.description = '';
            this.fileList = [];
            this.excelList = [];
            this.msgList = ["---开始---"];
            this.getPackerList();
            this.outerVisible = true;
        },
        /**
         * 日期列格式化
         * @param row
         * @param column
         * @param cellValue
         * @param index
         */
        dateTimeFormatter:function(row, column, cellValue, index){
            return dateFormatter(cellValue);
        },
        dateFormatter:function(row, column, cellValue, index){
            if (cellValue == null || cellValue == ''){
                return '';
            }
            return moment(cellValue).format('YYYY-MM-DD');
        },
        /**
         * 文件大小格式化
         */
        fileSizeFormatter:function(row, column, cellValue, index){
            return renderSize(cellValue);
        },
        /**
         * 点击确定按钮，弹出信息提示框，开始上传
         */
        handleTask:function(){
            let that = this;
            this.msgList = ["---开始---"];
            this.$refs.taskPack.validate(function(valid) {
                if (valid) {
                    if (that.excelList.length == 0){
                        noticeWarning(that,'必须上传Excel');
                        return false;
                    }
                    if (that.fileList.length == 0){
                        noticeWarning(that,'必须上传至少一个附件');
                        return false;
                    }
                    this.msgList = ["---开始---"];
                    that.innerVisible = true;
                    that.closeDialogButton=true;
                    that.$refs.uploadExcel.submit();
                } else {
                    return false;
                }
            });
        },
        /**
         * 添加Excel文件时
         * @param file
         * @param fileList
         */
        excelOnChange:function(file, fileList){
            this.excelList = fileList;
        },
        /**
         * 添加附件文件时
         * @param file
         * @param fileList
         */
        fileOnChange:function(file, fileList){
            this.fileList = fileList;
        },
        /**
         * 关闭弹出框
         */
        closeDialog:function(){
            this.innerVisible = false;
            this.excelList = [];
            
            if (this.msgList.indexOf("中止")>-1){
                this.outerVisible = true;
            }
            else {
                this.outerVisible = false;
            }
            this.initTaskPageList();
        },
        /**
         * Excel文件上传解析时
         * @param event
         * @param file
         * @param fileList
         */
        excelOnProcess:function(event, file, fileList){

            this.msgList.push("正在解析Excel数据...");
            this.progressPercent =  event.percent;
        },
        /**
         * Excel文件上传解析出错时
         * @param err
         * @param file
         * @param fileList
         */
        excelOnError:function(err, file, fileList){
            this.progressStatus = 'exception';
            this.msgList.push("解析Excel数据失败，失败原因："+err);
            this.msgList.push("中止");
            this.closeDialogButton = false;
        },
        /**
         * Excel文件上传解析成功时
         * @param response
         * @param file
         * @param fileList
         */
        excelOnSuccess:function(response, file, fileList){
            if (response.code == 0){
                this.progressStatus = 'success';
                this.msgList.push("解析Excel数据完成");
                this.fileHeader.relationId = response.data;
                this.$refs.uploadFile.submit();
            }
            else{
                this.progressStatus = 'exception';
                this.msgList.push("Excel数据解析错误");
                let that = this;
                response.data.errors.forEach(function (item) {
                    that.msgList.push(item);
                });
                this.msgList.push("中止");
                this.closeDialogButton = false;
            }
            this.fileNumber = 0;
        },
        /**
         * Excel文件上传之前
         * @param file
         */
        excelBeforeUpload:function(file){
            this.progressStatus = '';
            this.progressPercent = 0;
            this.msgList.push("开始上传并解析Excel数据");
        },
        /**
         * 附件文件上传保存时
         * @param event
         * @param file
         * @param fileList
         */
        fileOnProcess:function(event, file, fileList){
            this.msgList.push("正在上传【" + file.name + "】...");
            this.progressPercent =  event.percent;
        },
        /**
         * 附件 文件上传保存出错时
         * @param err
         * @param file
         * @param fileList
         */
        fileOnError:function(err, file, fileList){
            this.progressStatus = 'exception';
            this.msgList.push("上传失败，失败原因："+err);
            this.fileNumber ++ ;
            if (this.fileNumber == fileList.length){
                this.msgList.push("---结束---");
                this.closeDialogButton = false;
            }
        },
        uploadOnSuccess:function(response,file,fileList){
            if (response.code == 0){
                noticeSuccessMessage(this,"附件：【"+fileList[0].name+"】上传成功");
            }
            else{
                noticeError(this,"附件：【"+fileList[0].name+"】上传失败");
            }
        },
        /**
         * 附件文件上传保存成功时
         * @param response
         * @param file
         * @param fileList
         */
        fileOnSuccess:function(response, file, fileList){
            if (response.code == 0){
                this.progressStatus = 'success';
                this.msgList.push("【" + file.name + "】上传成功");
            }
            else{
                this.progressStatus = 'exception';
                let that = this;
                response.data.errors.forEach(function (item) {
                    that.msgList.push(item);
                })
            }
            this.fileNumber ++ ;
            if (this.fileNumber == fileList.length){
                this.msgList.push("---结束---");
                this.closeDialogButton = false;
            }
        },
        /**
         * 附件文件上传之前
         * @param file
         */
        fileBeforeUpload:function(file){
            this.progressStatus = '';
            this.progressPercent = 0;
            this.msgList.push("开始上传【" + file.name + "】");
        },
        /**
         * 查看附件列表
         * @param row
         */
        openFileList:function(row){
            let that = this;
            Axios.get('/fileList',
                {
                    params: {
                        id: row.id
                    }
                })
                .then(function (response) {
                    if (response.code != 0){
                        noticeDataRequestFailure(that);
                        return;
                    }
                    that.viewFileList = response.data;
                })
                .catch(function (error) {
                    noticeFailure(that);
                });
            this.dialogTableVisible = true;
        },

        /**
         * 删除附件列表
         * @param row
         */
        deleteAttachment:function(row){
            let that = this;
            Axios.get('/fileList',
                {
                    params: {
                        id: row.id
                    }
                })
                .then(function (response) {
                    if (response.code != 0){
                        noticeDataRequestFailure(that);
                        return;
                    }
                    that.viewFileList = response.data;
                })
                .catch(function (error) {
                    noticeFailure(that);
                });
            this.dialogTableDeleteVisible = true;
        },
        /**
         * 删除附件
         */
        deleteFile:function(row){
            let that = this;
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                Axios.get('/deleteFile',
                    {
                        params: {
                            id: row.id
                        }
                    })
                    .then(function (response) {
                        if (response.code != 0){
                            noticeDataRequestFailure(that);
                            return;
                        }
                        for (let i=0;i<that.viewFileList.length;i++)  {
                            if (that.viewFileList[i].id = row.id){
                                that.viewFileList.splice(i,1);
                                noticeSuccessMessage(that,'删除成功');
                                return;
                            }
                        }
                    })
                    .catch(function (error) {
                        noticeFailure(that);
                    });
            }).catch(() => {
                noticeInfo(that,'已取消删除');
            });
        },
        /**
         * 模板下载
         */
        downloadTemplate:function(){
            // axios.get(project +'/onlineArchive/outsourcing/downloadTemplate',
            //     {
            //         timeout: 300000,
            //     })
            //     .then(function (response) {
            //         console.log(response)
            //     });
            window.location.href = project +'/onlineArchive/outsourcing/downloadTemplate';
        },
        /**
         * 附件上传 点击上传按钮后执行
         */
        handleAttachmentUpload:function(){
            if (this.uploadFileList.length == 0){
                noticeWarning(this,"至少要选择一个附件");
                return;
            }
            this.$refs.attachmentUpload.submit();
            this.uploadVisible = false;
        },
        /**
         * 添加附件时执行
         * @param file
         * @param fileList
         */
        attachmentOnChange:function(file,fileList){
            this.uploadFileList = fileList;
        },
        /**
         * 查看附件/下载附件
         * @param row
         */
        viewFile:function(row){
            window.location.href = project +'/onlineArchive/outsourcing/download?id='+row.id;
        },

    }
}
new Vue(main);